<template>
	<view>
		<view class="formbox">
			<view class="formbox-item" @click="upload">
				<text class="title">头像</text>
				<image class="touxiang" :src="avatar"></image>
				<image class="right" src="../../static/usercenter/icon_zk.png"></image>
			</view>
			<view class="formbox-item1" @click="verify">
				<view class="title">真实姓名</view>
				<view class="value">
					<input type="text" v-model="username" disabled="false" placeholder-class="placeholderClass"
						placeholder="请填写您本人的真实姓名" />
				</view>
			</view>
			<view class="formbox-item1" @click="verify">
				<view class="title">身份证号</view>
				<view class="value">
					<input type="text" v-model="idcard" disabled="false" placeholder-class="placeholderClass"
						placeholder="请填写您本人的身份证号" />
				</view>
			</view>
			<view class="formbox-item1">
				<view class="title">昵称</view>
				<view class="value">
					<input type="text" v-model="nickname" placeholder-class="placeholderClass" placeholder="请输入昵称" />
				</view>
			</view>
			<view class="formbox-item1">
				<view class="title">身高</view>
				<view class="value">
					<input type="text" v-model="height" placeholder="请输入身高(单位cm)"  placeholder-class="placeholderClass"/>
					
				</view>
			</view>
			<view class="formbox-item1">
				<view class="title">体重</view>
				<view class="value">
				
					<input type="text" v-model="weight" placeholder="请输入体重(单位kg)"  placeholder-class="placeholderClass" />
				</view>
			</view>
			
			
			<view class="formbox-item1">
				<view class="title">是否求职</view>
				<view class="value">
					<picker @change="bindPickerJob" :value="index" :range="isJobList">
						<view class="uni-input">{{jobName}}</view>
					</picker>
					<image class="right" src="../../static/usercenter/icon_zk.png"></image>
				</view>
			</view>
	
			<view class="formbox-item1">
				<view class="title">工作年限</view>
				<view class="value">
					<picker @change="bindPickerTime" :value="index" :range="times">
						<view class="uni-input">{{time}}</view>
					</picker>
					<image class="right" src="../../static/usercenter/icon_zk.png"></image>
				</view>

			</view>
		</view>
		<view class="fiexd-btn">
			<button @click="update()">修改</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			const currentDate = this.getDate({
				format: true
			})
			return {
				array1: ['请选择身高', '160', '170', '180'],
				array2: ['请选择体重', '160', '170', '180'],
				array3: ['请选择工种', '160', '170', '180'],
				array4: ['请选择工作地点1', '160', '170', '180'],
				array5: ['请选择工作地点2', '160', '170', '180'],
				index: 0,
				date: "请选择从业时间",
				isVerify: false,
				idcard: '',
				username: '',
				avatar:'',
				uploadAvatar:'',
				nickname:'',
				weight:'',
				height:'',
				typeList:[],
				typeNameList:[],
				workId:'',
				typeName:'请选择工种',
				address1:'',
				address2:'',
				addressName1:'请选择工作地1',
				addressName2:'请选择工作地2',
				isJobList:['在职','求职'],
				jobName:'求职',
				times:[1,2,3,4,5,6,7,8,9,10],
				time:1
				
			}
		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}
		},
		onLoad(){
			this.avatar = this.$util.$getStorageSync("avatar");	
			
			this.myWorkList();
		},

		onShow() {
			this.address1 = this.$util.$getStorageSync("address1");
			this.address2 = this.$util.$getStorageSync("address2");
			console.info("address1=="+this.address1)
			if(this.address1 != ''){
				this.addressName1 = this.$util.$getStorageSync("address1Value");
			}
			if(this.address2 != ''){
				this.addressName2 = this.$util.$getStorageSync("address2Value");
			}
			this.verifyInfo();
			this.profile();
		},
		methods: {
			
			bindPickerTime(e){
				var index =  e.target.value
				this.time = this.times[index];
			},
			bindPickerType(e){
				var index =  e.target.value
				this.workId = this.typeList[index].id;
				this.typeName = this.typeList[index].workname;
				console.info("===workId==="+this.workId);
			},
			bindPickerJob(e){
				var index =  e.target.value
				this.jobName = this.isJobList[index];
			},
			profile(){
				let param = {};
				let that = this;
				this.$api.myProfile(param).then(res => {
				
					if (res.code == 1) {
						
						if(res.data != null){
							that.nickname = res.data.nickname;
							that.height = res.data.height;
							that.weight = res.data.weight;
							that.typeName = res.data.work.workname;
							that.jobName = res.data.jobStatus;
							that.time = res.data.time;
							if(res.data.address1 != null){
								that.addressName1 = res.data.address1.provinceName+res.data.address1.cityName+res.data.address1.areaName+res.data.address1.detail;
							}
							if(res.data.address2 != null){
								that.addressName2 = res.data.address2.provinceName+res.data.address2.cityName+res.data.address2.areaName+res.data.address2.detail;
							}
							that.date =  res.data.time;
						}
					} 
					
					console.info(res)
				})
			},
			
			addAddress(type){
			
				uni.navigateTo({
					url:'./addAddress?type='+type
				})
			},
			 

			upload() {

				let that = this;
				uni.chooseImage({
					success: (chooseImageRes) => {
						// 获取的格式是数组，多选会同时返回，单选只返回一项
						const tempFilePaths = chooseImageRes.tempFilePaths;
						// 若多选，需循环调用uni.uploadFile ，因微信小程序只支持单文件上传
						uni.uploadFile({
							url: 'https://bs.50xue.com/api/common/upload', //仅为示例，非真实的接口地址
							filePath: tempFilePaths[0],
							name: 'file',
							success: (uploadFileRes) => {
								var res = uploadFileRes.data;
								
								// 根据接口具体返回格式   赋值具体对应url
								that.avatar = JSON.parse(res).data.fullurl;
								var avatar = JSON.parse(res).data.fullurl;
								that.$util.$setStorageSync("avatar",avatar);
								console.info("===avatar=="+avatar);
							
								that.uploadAvatar = JSON.parse(res).data.url;
								that.update();
								
								
							}
						});
					}
				});
			},
			verify() {

				if (this.isVerify) {
					return;
				}
				uni.navigateTo({
					url: './realname'
				})
			},
			
			myWorkList(){
				
				let param = {};
				let that = this;
				let typeNameList = [];
				this.$api.worktype(param).then(res => {
				
					if (res.code == 1) {
						that.typeList = res.data;
						for(var i=0;i<res.data.length;i++){
							typeNameList.push(res.data[i].workname)
						}
					} 
					
					console.info(res)
				})
				this.typeNameList = typeNameList;
			},
			
			update(){
			
				let param = {
					
				}
				if(this.uploadAvatar){
					param.avatar = this.uploadAvatar
				}
				if(this.nickname){
					param.nickname = this.nickname
				}
				if(this.weight){
					param.weight = this.weight
				}
				if(this.height){
					param.height = this.height
				}
				if(this.workId){
					param.workId = this.workId
					
				}
				if(this.address1){
					param.addressId = this.address1
				}
				if(this.address2){
					param.addressId2 = this.address2
				}
				
				if(this.jobName){
					param.jobStatus = this.jobName;
				}
				param.workTime = this.time;
				let that = this;
				this.$api.updateprofile(param).then(res => {
				
					if (res.code == 1) {
						that.$util.$setStorageSync("address1", "");
						that.$util.$setStorageSync("address2", "");
						that.$util.$setStorageSync("address2value", "");
						that.$util.$setStorageSync("address1value", "");
						that.$util.$showToast("修改成功", '', 'none');
						
					} else {
						that.isVerify = false;
					}
					console.info(res)
				})
			},

			verifyInfo() {
				let param = {

				}
				let that = this;
				this.$api.verifyInfo(param).then(res => {

					if (res.code == 1) {
						that.isVerify = true;
						that.idcard = res.data.idcard;
						that.username = res.data.username;

					} else {
						that.isVerify = false;
					}
					console.info(res)
				})
			},

			bindDateChange: function(e) {
				this.date = e.target.value
			},
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();

				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			}
		}
	}
</script>

<style lang="scss">
	.formbox {
		padding-bottom: 140rpx;

		.formbox-item {
			margin: 0 40rpx;
			padding: 35rpx 0;
			display: flex;
			border-bottom: 1rpx solid #e0e0e0;

			.title {
				flex: auto;
				font-size: 28rpx;
				color: #333333;
			}

			.touxiang {
				width: 128rpx;
				height: 128rpx;
			}

			.right {
				width: 36rpx;
				height: 36rpx;
				margin-left: 36rpx;
				margin-top: 47rpx;
			}
		}

		.formbox-item1 {
			margin: 0 40rpx;
			padding: 35rpx 0;
			border-bottom: 1rpx solid #e0e0e0;

			.title {
				font-size: 28rpx;
				color: #333333;
				margin-bottom: 54rpx;
			}

			.value {
				display: flex;

				picker {
					font-size: 28rpx;
					color: #BFBFBF;
					flex: auto;
				}

				.right {
					width: 36rpx;
					height: 36rpx;
				}
			}

		}
	}
</style>
